package com.example.webapp.controller;

import com.example.webapp.entity.RequestInterface;
import com.example.webapp.repository.RequestInterfaceRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/request-interfaces")
public class RequestInterfaceController {

    @Autowired
    private RequestInterfaceRepository requestInterfaceRepository;

    @GetMapping
    public ResponseEntity<?> getInterfaces(@RequestParam Long groupId) {
        try {
            return ResponseEntity.ok(requestInterfaceRepository.findByGroupId(groupId));
        } catch (Exception e) {
            log.error("Error getting interfaces", e);
            return ResponseEntity.badRequest().body("Error getting interfaces");
        }
    }

    @PostMapping
    public ResponseEntity<?> createInterface(@RequestBody RequestInterface requestInterface) {
        try {
            return ResponseEntity.ok(requestInterfaceRepository.save(requestInterface));
        } catch (Exception e) {
            log.error("Error creating request interface", e);
            return ResponseEntity.badRequest().body("Error creating request interface");
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getInterface(@PathVariable Long id) {
        try {
            return ResponseEntity.ok(requestInterfaceRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Interface not found")));
        } catch (Exception e) {
            log.error("Error getting interface", e);
            return ResponseEntity.badRequest().body("Error getting interface");
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteInterface(@PathVariable Long id) {
        try {
            requestInterfaceRepository.deleteById(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("Error deleting interface", e);
            return ResponseEntity.badRequest().body("Error deleting interface");
        }
    }
} 